<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="90px">
      <el-form-item label="样本库" prop="name">
        <el-input
          v-model="queryParams.name"
          clearable
          placeholder="请输入样本库"
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属分类" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请选择所属分类">
          <el-option
            v-for="item in typeOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleDelete"
        >
          批量删除
        </el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList(),getTypeList()" />
    </el-row>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="所属指标分类" prop="id">
        <template slot-scope="scope">
          <span v-for="item in typeOptions" :key="item.id">
            <span v-if="item.id == scope.row.typeId">{{ item.name }}</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" align="center" label="样本库" prop="name" />
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
          <el-button
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="gotoSample(scope.row)"
          >
            样本管理
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pagesize"
      :page.sync="queryParams.pagenum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="样本库" prop="name">
          <el-input v-model="form.name" maxlength="20" placeholder="请输入样本库" />
        </el-form-item>
        <el-form-item label="所属分类" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择所属分类">
            <el-option
              v-for="item in typeOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">
          确 定
        </el-button>
        <el-button @click="cancel">
          取 消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getToken} from "@/utils/auth"
import {
  typeList,
  saveLibrary,
  deleteLibrary,
  libraryDetail,
  getLibraryList,
  updateLibrary
} from '@/api/sampleCenterManagement'
export default {
  name: 'SampleLibrary',
  components:{
  },
  data() {
    let handleName = (rule, value, callback) => {
      let validator ='^[A-Za-z0-9\u4e00-\u9fa5]+$'
      if (value) {
        if(!new RegExp(validator).test(value)){
          callback(new Error('请输入中英文+阿拉伯数字格式'))
        }else{
          callback()
        } 
      }else{
        callback(new Error('样本库不能为空'))
      }
        
    }
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        pagenum: 1,
        pagesize: 10,
        name:undefined,
        typeId: undefined,
      },
      // 问卷参数
      form: {},
      // 问卷校验
      rules: {
        typeId: [
          {required: true, message:'请选择所属分类',trigger: 'change'}
        ],
        name: [
          {required: true, validator:handleName,trigger: 'blur'}
        ],
      },
      
    }
  },
  created() {
    this.getList()
    this.getTypeList()
  },
  methods: {
    getTypeList(){
      typeList({}).then((response) => {
        this.typeOptions = response.data
      })
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true
      getLibraryList(this.queryParams).then((response) => {
          this.configList = response.data.records
          this.total = response.data.total
          this.loading = false
        }
      )
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 问卷重置
    reset() {
      this.form = {
        typeId: undefined,
        name: undefined,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pagenum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增'
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      libraryDetail(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateLibrary(this.form).then((response) => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            saveLibrary(this.form).then((response) => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return deleteLibrary(ids)
      }).then(() => {
        var pageSize =Math.ceil((this.total-(row.id ? 1 : this.ids.length ))/this.queryParams.pagesize)
        if(pageSize < this.queryParams.pagenum){
            this.queryParams.pagenum = pageSize
        }
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    gotoSample(row){
      this.$router.push({path:'/sample',query:{sampleLibraryId :row.id}})
    }
  }
}
</script>
